package logic

import (
	"context"
	"errors"
	"shop/model/mysql"

	"shop/internal/svc"
	"shop/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseListLogic {
	return &HouseListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseListLogic) HouseList(in *shop.HouseListRequest) (*shop.HouseListResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var h mysql.House
	if err = h.GetByUserId(); err != nil {
		logx.Error("用户未发布房源")
		return nil, errors.New("用户未发布房源")
	}
	var hs []mysql.House
	mysql.DB.Find(&hs)
	var houseList []*shop.HouseList
	for _, k := range hs {
		if k.Examine != 1 {
			logx.Error("审核未通过不能进行展示")
			return nil, errors.New("审核未通过不能进行展示")
		}
		houseList = append(houseList, &shop.HouseList{
			Image: k.Image,
			Title: k.Title,
			Price: float32(k.Price),
			Num:   int64(k.Num),
			Info:  k.Info,
		})
	}
	return &shop.HouseListResponse{
		List: houseList,
	}, nil
}
